package com.d.lib.ui.view.recordtrigger;


import com.d.lib.ui.view.ResourceTable;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

/**
 * 触发器
 * Created by D on 2017/11/1.
 */
public class RecordTriggerView extends Text implements Component.EstimateSizeListener,
    Component.TouchEventListener {
  public static final int STATE_CANCLE = 0; // 滑出范围，取消本次录音
  public static final int STATE_VALID = 1; // 有效，开始录音
  public static final int STATE_INVALID = 2; // 无效，滑出范围
  public static final int STATE_SUBMIT = 3; // 未滑出范围，提交本次录音

  private int mWidth;
  private int mHeight;

  private int mTouchRadius; // Touch有效范围，半径
  private float mDX, mDY;
  private boolean mIsValid; // Touch是否有效，在有效范围内
  private OnTriggerListener mListener;
  private int mCurState;

  public RecordTriggerView(Context context) {
    super(context);
    init(context);
    setTouchEventListener(this);
    setEstimateSizeListener(this);
  }

  public RecordTriggerView(Context context, AttrSet attrs) {
    super(context, attrs);
    init(context);
    setTouchEventListener(this);
    setEstimateSizeListener(this);
  }

  public RecordTriggerView(Context context, AttrSet attrs, String defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);

    setTouchEventListener(this);
    setEstimateSizeListener(this);
  }

  private void init(Context context) {
    mTouchRadius = AttrHelper.vp2px(105, getContext());
    setTextColor(Color.GRAY);
    setText("按住 说话");
    setTextSize(14, TextSizeType.VP);
    setTextAlignment(TextAlignment.CENTER);
    ShapeElement shapeElement =
        (ShapeElement) ElementScatter.getInstance(getContext()).parse(ResourceTable.Layout_lib_ui_view_rtv_corner_bg_normal);
    setBackground(shapeElement);
  }


  public void setOnTriggerListener(OnTriggerListener l) {
    this.mListener = l;
  }


  public interface OnTriggerListener {
    void onStateChange(int state);
  }

  @Override
  public boolean onEstimateSize(int widthMeasureSpec, int heightMeasureSpec) {
    mWidth = MeasureSpec.getSize(widthMeasureSpec);
    mHeight = MeasureSpec.getSize(heightMeasureSpec);
    setEstimatedSize(mWidth, mHeight);
    return false;
  }

  @Override
  public boolean onTouchEvent(Component component, TouchEvent event) {
    MmiPoint point = event.getPointerPosition(event.getIndex());
    float eX = point.getX();
    float eY = point.getY();
    switch (event.getAction()) {
      case TouchEvent.PRIMARY_POINT_DOWN:
        mDX = eX;
        mDY = eY;
        mIsValid = true;
        mCurState = STATE_VALID;
        ShapeElement vectorElement = new ShapeElement(getContext(),
            ResourceTable.Layout_lib_ui_view_rtv_corner_bg_select);
        setBackground(vectorElement);
        setText("松开 结束");
        if (mListener != null) {
          mListener.onStateChange(mCurState);
        }
        return true;
      case TouchEvent.POINT_MOVE:
        mIsValid =
            Math.abs(Math.sqrt((eX - mDX) * (eX - mDX) + (eY - mDY) * (eY - mDY))) < mTouchRadius;
        int state = mIsValid ? STATE_VALID : STATE_INVALID;
        if (state != mCurState) {
          setText(mIsValid ? "松开 结束" : "松开手指 取消发送");
          if (mListener != null) {
            mListener.onStateChange(state);
          }
        }
        mCurState = state;
        return true;
      case TouchEvent.CANCEL:
      case TouchEvent.PRIMARY_POINT_UP:
        mCurState = mIsValid ? STATE_SUBMIT : STATE_CANCLE;
        if (mListener != null) {
          mListener.onStateChange(mCurState);
        }
        ShapeElement vectorElement1 = new ShapeElement(getContext(),
            ResourceTable.Layout_lib_ui_view_rtv_corner_bg_normal);
        setBackground(vectorElement1);
        setText("按住 说话");
        return true;
    }
    return super.getTouchEventListener().onTouchEvent(component, event);
  }

}
